package com.tz.online.order.service.impl;

import java.util.List;

import com.tz.online.entity.Address;
import com.tz.online.entity.Order;
import com.tz.online.entity.Pageing;
import com.tz.online.entity.User;
import com.tz.online.order.dao.IOrderDao;
import com.tz.online.order.service.IOrderService;
import com.tz.online.util.BeanFactory;

public class OrderServiceImpl implements IOrderService {
	IOrderDao od = (IOrderDao) BeanFactory.getBean("orderDao");
	@Override
	public Pageing queryAllAddressByUser(User user,int size,int now) {
		Pageing paging = new Pageing();
		List<Address> address = od.selectAddressByPage(user, size, now);
		paging.setAs(address);
		paging.setPageNow(now);
		paging.setPageSize(size);
		int rowCount = (int) od.getRowCount(user, "online_address");
		paging.setRowCount(rowCount);
		int pageCount = 0;
		if (rowCount % size == 0) {
			pageCount = rowCount / size;
		} else {
			pageCount = rowCount / size + 1;
		}

		paging.setPageCount(pageCount);
		return paging;
	}

	@Override
	public void saveAddress(Address a) {
		od.insertAddress(a);
	}

	@Override
	public void saveOrder(Order o) {
		// TODO Auto-generated method stub
		od.insertOrder(o);
	}

	@Override
	public Pageing queryOrderByPage(User user,int size, int now) {
		Pageing paging = new Pageing();
		List<Order> orders = od.selectOrderByPage(user, size, now);
		paging.setOrders(orders);
		paging.setPageNow(now);
		paging.setPageSize(size);
		int rowCount = (int) od.getRowCount(user, "online_address");
		paging.setRowCount(rowCount);
		int pageCount = 0;

		if (rowCount % size == 0) {
			pageCount = rowCount / size;
		} else {
			pageCount = rowCount / size + 1;
		}
		paging.setPageCount(pageCount);
		return paging;
	}

	@Override
	public Address queryAddressById(String aid) {
		// TODO Auto-generated method stub
		return od.selectAddressById(aid);
	}
	
	

}
